var express = require('express');
var router = express.Router();
const usersApi = require("../api/users");

/**,
 * @swagger
 * /users/login:
 *    post:
 *      tags:
 *      - 用户          # 接口分类
 *      summary: 用户登录接口   # 接口备注
 *      description: 用户登录   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              $ref: '#/components/schemas/users'
 *            example:
 *              username: "zhangsan"
 *              userPassword: "123456"
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "登录成功"     # 返回 true
 *          content:
 *            application/json:
 *              schema:
 *                $ref: '#/components/schemas/users'
 * */
router.post('/login', async function (req, res, next) {
    const { userName, userPassword } = req.body
    const data = await usersApi.login({ userName, userPassword })
    res.send(data)
});


/**,
 * @swagger
 * /users/reg:
 *    post:
 *      tags:
 *      - 用户          # 接口分类
 *      summary: 用户注册接口   # 接口备注
 *      description: 用户注册   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      requestBody:
 *        required: true
 *        content:
 *          application/json:
 *            schema:
 *              $ref: '#/components/schemas/users'
 *            example:
 *              username: "zhangsan"
 *              userPassword: "123456"
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "注册成功"     # 返回 true
 *          content:
 *            application/json:
 *              schema:
 *                $ref: '#/components/schemas/users'
 * */
router.post('/reg', async function (req, res, next) {
    const { userName, userPassword } = req.body
    console.log({ userName, userPassword });
    const data = await usersApi.reg({ userName, userPassword })
    if (data._id) {
        res.send(true)
    }
});


/**
 * @swagger
 * /users/checkName:
 *    get:
 *      tags:
 *      - 用户          # 接口分类
 *      summary: 用户名字查重   # 接口备注
 *      description: 用户输入的姓名是否重复   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
*      parameters:
 *        - in: query
 *          name: username
 *          schema:
 *            type: string
 *            default: zhangsan
 *          description: 用户名
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "查询成功" # 返回 code 码描述
 *          content:
 *            application/json:
 *              schema:
 *                type: object
 * 
 * */
router.get('/checkName', async function (req, res, next) {
    const { userName } = req.query;
    const data = await usersApi.checkName({ userName })
    data ? res.send(true) : res.send(false)
});



/**
 * @swagger
 * /users/getList:
 *    get:
 *      tags:
 *      - 用户          # 接口分类
 *      summary: 用户列表   # 接口备注
 *      description: 返回用户数据   # 接口描述
 *      consumes:
 *      - "application/json"    # 接口接收参数类型
 *      responses:  # 响应
 *        200:     # http状态码
 *          description: "查询成功" # 返回 code 码描述
 *          content:
 *            application/json:
 *              schema:
 *                type: object
 *                properties:
 *                  _id:
 *                    type: string
 *                    description: 用户唯一编码
 *                  name:
 *                    type: string
 *                    description: 用户姓名
 * 
 * */
router.get('/getList', async function (req, res, next) {
    const { curPage, eachPage } = req.query;
    const data = await usersApi.getList({ curPage, eachPage });
    res.send(data)
});


module.exports = router;
